package com.feather.core;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;
import com.feather.annotation.*;

public class ActionUnitMethod implements ActionUnit {

	private Object object;
	
	private Method method;
	
	public ActionUnitMethod(Object object, Method method)
	{
		this.object = object;
		this.method = method;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Object execute(Object input) throws Exception {
		
		Annotation[][] annoss = method.getParameterAnnotations();
		
		Object[] params = null;
		
		if(input instanceof Map)
		{
			Map<String, Object> inputMap = (Map<String, Object>) input;
			params = new Object[annoss.length];
			
			for(int i = 0; i < params.length; i++)
			{
				for(Annotation a : annoss[i])
				{
					if(a.annotationType().equals(InputParam.class))
					{
						InputParam inputParamAnno = (InputParam) a;
						params[i] = inputMap.get(inputParamAnno.name());
						
						break;
					}
				}
			}
		}
		else if(annoss.length == 1)
		{
			
		}
		
		return method.invoke(object, params);
	}

}
